package com.fred.util


import java.io.File

object Util {

    private val localPath:String by lazy {
        //SpecialFolderPath:C:\Users\{userName}\AppData\Local
        LocalApi.getSpecialFolderPath()
    }

    private val isCanWrite by lazy {
        val path = System.getProperty("user.dir")
        if (path != null) {
            val lp = "$path${File.separator}log${File.separator}"
            var result = LocalApi.canWrite(lp)
            if (!result) {
                val file = File(lp)
                if (!file.exists()){
                    file.mkdirs()
                }
            }
            result = LocalApi.canWrite(lp)
            result
        } else {
            false
        }
    }

    val resourcesPath: String? by lazy {
        val resourcePath = System.getProperty("compose.application.resources.dir")
        if (resourcePath == null) {
            val root = System.getProperty("user.dir")
            "${root}${File.separator}appData${File.separator}${currentOsName}${File.separator}"
        } else {
            "${resourcePath}${File.separator}"
        }
    }

    val logPath:String by lazy {
        val path = System.getProperty("user.dir")
        if (path != null) {
            val lp = "$path${File.separator}log${File.separator}log"
            if (isCanWrite) {
                lp
            } else {
                "${localPath}${File.separator}installTool${File.separator}log${File.separator}log"
            }
        } else {
            "${localPath}${File.separator}installTool${File.separator}log${File.separator}log"
        }
    }

    val logCachePath:String by lazy {
        val path = System.getProperty("user.dir")
        if (path != null) {
            val lcp = "$path${File.separator}log${File.separator}cache"
            if (isCanWrite) {
                lcp
            } else {
                "${localPath}${File.separator}installTool${File.separator}log${File.separator}cache"
            }
        } else {
            "${localPath}${File.separator}installTool${File.separator}log${File.separator}cache"
        }
    }

    val propertiesPath:String by lazy {
        val path = System.getProperty("user.dir")
        if (path != null) {
            val lcp = "$path${File.separator}app${File.separator}"
            if (isCanWrite) {
                lcp
            } else {
                "${localPath}${File.separator}installTool${File.separator}properties${File.separator}"
            }
        } else {
            "${localPath}${File.separator}installTool${File.separator}properties${File.separator}"
        }
    }

    private val currentOsName: String by lazy {
        val os = System.getProperty("os.name")
        when {
            os.equals("Mac OS X", ignoreCase = true) -> "macos"
            os.startsWith("Win", ignoreCase = true) -> "windows"
            os.startsWith("Linux", ignoreCase = true) -> "linux"
            else -> ""
        }
    }




}